# PowerShell脚本执行SQL文件
Write-Host "正在执行SQL文件创建数据库表..." -ForegroundColor Green

# 尝试不同的MySQL路径
$mysqlPaths = @(
    "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe",
    "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe", 
    "C:\xampp\mysql\bin\mysql.exe",
    "C:\wamp64\bin\mysql\mysql8.0.21\bin\mysql.exe",
    "mysql"  # 如果在PATH中
)

$mysqlFound = $false

foreach ($path in $mysqlPaths) {
    try {
        if ($path -eq "mysql") {
            # 尝试直接使用mysql命令
            $result = & mysql --version 2>$null
            if ($LASTEXITCODE -eq 0) {
                Write-Host "找到MySQL客户端: mysql" -ForegroundColor Yellow
                Get-Content "create_multitool_tables.sql" | & mysql -h localhost -P 3306 -u root -p123456
                if ($LASTEXITCODE -eq 0) {
                    Write-Host "数据库表创建成功！" -ForegroundColor Green
                    $mysqlFound = $true
                    break
                }
            }
        } elseif (Test-Path $path) {
            Write-Host "找到MySQL客户端: $path" -ForegroundColor Yellow
            Get-Content "create_multitool_tables.sql" | & $path -h localhost -P 3306 -u root -p123456
            if ($LASTEXITCODE -eq 0) {
                Write-Host "数据库表创建成功！" -ForegroundColor Green
                $mysqlFound = $true
                break
            }
        }
    }
    catch {
        # 继续尝试下一个路径
    }
}

if (-not $mysqlFound) {
    Write-Host "未找到MySQL客户端，请手动执行以下命令：" -ForegroundColor Red
    Write-Host "mysql -h localhost -P 3306 -u root -p123456 < create_multitool_tables.sql" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "或者使用MySQL Workbench等图形化工具执行SQL文件" -ForegroundColor Yellow
}
